#include "StdAfx.h"
#include "ProxyDao.h"


CProxyDao::~CProxyDao(void)
{
}


Proxy CProxyDao::GetProxy()
{
	Proxy Temp;
	CppSQLite3DB db;
	try
	{
		wstring dbPath = AppInit.GetDbFilePath();
		db.open(dbPath.c_str());		
		CppSQLite3Query q = db.execQuery(_T("select * from Proxy;"));
		while (!q.eof())
		{
			ProxyNode ProxyTemp;
			ProxyTemp.ProxyName = q.fieldValue(1);
			ProxyTemp.ProxyIp = q.fieldValue(2);
			ProxyTemp.ProxyPort = q.fieldValue(3);
			ProxyTemp.ProxyType = q.fieldValue(4);
			ProxyTemp.ProxyUserName = q.fieldValue(5);
			ProxyTemp.ProxyPwd = q.fieldValue(6);
			Temp.push_back(ProxyTemp);
			q.nextRow();
		}
		q.finalize();
	}
	catch(CppSQLite3Exception ex)
	{
		::MessageBox(NULL, (LPCWSTR)ex.errorMessage(), (LPCWSTR)"", MB_OK);
	}
	db.close();
	return Temp;
}

ProxyNode CProxyDao::GetOneProxy(const wstring &ProxyName)
{
	ProxyNode Temp;
	CppSQLite3DB db;
	try
	{
		wstring dbPath = AppInit.GetDbFilePath();
		db.open(dbPath.c_str());		
		CString strSql;
		strSql.Format(L"select * from Proxy where ProxyName='%s';",ProxyName.c_str());
		CppSQLite3Query q = db.execQuery(strSql.GetBuffer(0));
		if (!q.eof())
		{			
			Temp.ProxyName = q.fieldValue(1);
			Temp.ProxyIp = q.fieldValue(2);
			Temp.ProxyPort = q.fieldValue(3);
			Temp.ProxyType = q.fieldValue(4);
			Temp.ProxyUserName = q.fieldValue(5);
			Temp.ProxyPwd = q.fieldValue(6);
		}
		q.finalize();
	}
	catch(CppSQLite3Exception ex)
	{
		::MessageBox(NULL, (LPCWSTR)ex.errorMessage(), (LPCWSTR)"", MB_OK);
	}
	db.close();
	return Temp;
}

BOOL CProxyDao::SetProxy(const ProxyNode &ProxyName)
{
	if (ProxyName.ProxyIp.empty())
	{
		return FALSE;
	}
	CppSQLite3DB db;
	try
	{
		wstring dbPath = AppInit.GetDbFilePath();
		db.open(dbPath.c_str());
		CString strSql;
		strSql.Format(L"select * from Proxy where ProxyName='%s' and ProxyIp='%s' and ProxyPort='%s' and ProxyType='%s' and ProxyUserName='%s'  and ProxyPwd='%s';",ProxyName.ProxyName.c_str(), ProxyName.ProxyIp.c_str(), ProxyName.ProxyPort.c_str(), ProxyName.ProxyType.c_str(), ProxyName.ProxyUserName.c_str(), ProxyName.ProxyPwd.c_str());
		CppSQLite3Query q = db.execQuery(strSql.GetBuffer(0));
		if (!q.eof())
		{
			strSql.Format(L"delete from Proxy where ProxyName='%s' and ProxyIp='%s' and ProxyPort='%s' and ProxyType='%s' and ProxyUserName='%s'  and ProxyPwd='%s';",ProxyName.ProxyName.c_str(), ProxyName.ProxyIp.c_str(), ProxyName.ProxyPort.c_str(), ProxyName.ProxyType.c_str(), ProxyName.ProxyUserName.c_str(), ProxyName.ProxyPwd.c_str());	
			db.execDML(strSql);
		}	
		strSql.Format(_T("insert into Proxy values(null, '%s', '%s', '%s', '%s', '%s', '%s');"),ProxyName.ProxyName.c_str(), ProxyName.ProxyIp.c_str(), ProxyName.ProxyPort.c_str(), ProxyName.ProxyType.c_str(), ProxyName.ProxyUserName.c_str(), ProxyName.ProxyPwd.c_str());
		if (SQLITE_OK != db.execDML(strSql))
		{
			return FALSE;
		}		
	}
	catch(CppSQLite3Exception ex)
	{
		::MessageBox(NULL, (LPCWSTR)ex.errorMessage(), (LPCWSTR)"", MB_OK);
	}
	db.close();
	return TRUE;	
}

BOOL CProxyDao::IsProxyExist(const wstring &ProxyName)
{
	BOOL bIsExist = FALSE;
	CppSQLite3DB db;
	try
	{
		wstring dbPath = AppInit.GetDbFilePath();
		db.open(dbPath.c_str());		
		CString strSql;
		strSql.Format(L"select * from Proxy where ProxyName='%s';",ProxyName.c_str());
		CppSQLite3Query q = db.execQuery(strSql.GetBuffer(0));
		if (!q.eof())
		{			
			 bIsExist = TRUE;
		}
		else
			 bIsExist = FALSE;
		q.finalize();
	}
	catch(CppSQLite3Exception ex)
	{
		::MessageBox(NULL, (LPCWSTR)ex.errorMessage(), (LPCWSTR)"", MB_OK);
	}
	db.close();
	return bIsExist;
}

int CProxyDao::GetProxyCount()
{
	int Temp = 0;
	CppSQLite3DB db;
	try
	{
		wstring dbPath = AppInit.GetDbFilePath();
		db.open(dbPath.c_str());	
		Temp = db.execScalar(L"select count(*) from Proxy;");
	}
	catch(CppSQLite3Exception ex)
	{
		::MessageBox(NULL, (LPCWSTR)ex.errorMessage(), (LPCWSTR)"", MB_OK);
	}
	db.close();
	return Temp;
}